package com.zuxia.impDao;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zuxia.iDao.PositioninfoDAOI;
import com.zuxia.po.Positioninfo;
import com.zuxia.po.Recruitinfo;

/**
 * 职位表Dao实现类
 * @author 谌华勇
 *
 * 2018年11月14日上午9:58:08
 */
public class PositioninfoDAO extends HibernateDaoSupport implements PositioninfoDAOI {
	private static final Logger log = LoggerFactory
			.getLogger(PositioninfoDAO.class);
	// property constants
	public static final String PT_INAME = "ptIname";
	public static final String PT_IREQUIRE = "ptIrequire";

	protected void initDao() {
		// do nothing
	}

	public void save(Positioninfo transientInstance) {
		log.debug("saving Positioninfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Positioninfo persistentInstance) {
		log.debug("deleting Positioninfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Positioninfo findById(java.lang.Integer id) {
		log.debug("getting Positioninfo instance with id: " + id);
		try {
			Positioninfo instance = (Positioninfo) getHibernateTemplate().get(
					"com.zuxia.po.Positioninfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Positioninfo instance) {
		log.debug("finding Positioninfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.PositioninfoDAOI#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Positioninfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Positioninfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.PositioninfoDAOI#findByPtIname(java.lang.Object)
	 */
	public List findByPtIname(Object ptIname) {
		return findByProperty(PT_INAME, ptIname);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.PositioninfoDAOI#findByPtIrequire(java.lang.Object)
	 */
	public List findByPtIrequire(Object ptIrequire) {
		return findByProperty(PT_IREQUIRE, ptIrequire);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.PositioninfoDAOI#findAll()
	 */
	public List findAll() {
		log.debug("finding all Positioninfo instances");
		try {
			String queryString = "from Positioninfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/**
	 * 只查询部门的id和name
	 * 谌华勇
	 *2018年11月14日上午10:07:07
	 */
	public List showPositionOnlyIdAndName(String dpIid) {
		log.debug("show all Positioninfo instances");
		try {
			String queryString = "select new Positioninfo(ptIid,ptIname) from Positioninfo";
			if(!dpIid.equals("")) {//代表根据部门id查询对应空闲职业(由于员工是员工职业则字段是Null，这里有case判断让其null为0)select new Positioninfo(ptIid,ptIname) from Positioninfo as a where a.ptIid not in (select positioninfo.ptIid from Staffindo b where departmentinfo.dpIid=? case when positioninfo.ptIid is null then 0 end)
				queryString = "select ptIid,ptIname from positioninfo as a where a.ptIId not in (select if(isnull(sIptIId),0,sIptIId) from staffindo where sIdpIId=:sIdpIId)";
				Session s = this.getHibernateTemplate().getSessionFactory().openSession();
				Query q = s.createSQLQuery(queryString);
				q.setInteger("sIdpIId", Integer.parseInt(dpIid));
				return q.list();
			}
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("show all failed", re);
			throw re;
		}
	}

	public Positioninfo merge(Positioninfo detachedInstance) {
		log.debug("merging Positioninfo instance");
		try {
			Positioninfo result = (Positioninfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Positioninfo instance) {
		log.debug("attaching dirty Positioninfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Positioninfo instance) {
		log.debug("attaching clean Positioninfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PositioninfoDAOI getFromApplicationContext(
			ApplicationContext ctx) {
		return (PositioninfoDAOI) ctx.getBean("PositioninfoDAO");
	}
	
	
	//重新写的根据id查
		public Positioninfo showIdPositioninfo(java.lang.Integer id) {
			log.debug("getting Positioninfo instance with id: " + id);
			try {
				String queryString = "select new Positioninfo(ptIid,ptIname,ptIrequire) from Positioninfo where ptIid="+id;
				return (Positioninfo) getSession().createQuery(queryString).uniqueResult();
			} catch (RuntimeException re) {
				log.error("get failed", re);
				throw re;
			}
		}
		
		//重新写的查询所有方法
		public List showPositioninfo() {
			log.debug("show all Positioninfo instances");
			try {
				String queryString = "select new Positioninfo(ptIid,ptIname,ptIrequire) from Positioninfo";
				return getHibernateTemplate().find(queryString);
			} catch (RuntimeException re) {
				log.error("show all failed", re);
				throw re;
			}
		}
		
		/**
		 * 张志鸿
		 */
		//添加方法
		public Boolean add(Positioninfo positioninfo) {
			Boolean b=true;
			log.debug("saving Positioninfo instance");
			try {
				getHibernateTemplate().save(positioninfo);
				log.debug("save successful");	
			} catch (RuntimeException re) {
				b=false;
				log.error("save failed", re);
				throw re;	
			}
			return b;
		}
}